package cn.com.sbell.vss;

import javax.servlet.http.HttpServletRequest;

/**
 * Response
 * 
 * @author Yifu.HUANG
 */
public class Response {
	// private String transactionId;
	//
	// private String sessionId;
	//
	// private String clientHost;
	//
	// private int clientPort;

	private String command;

	private String returnCode;

	private String returnMessage;

	private Object parameters;
	private String[] fields;

	public String[] getFields() {
		return fields;
	}

	public void setFields(String[] fields) {
		this.fields = fields;
	}

	public static Response createResponse(HttpServletRequest request) {
		Response response = new Response();
		// set return code to zero by default
		response.setReturnCode(VssConstants.CODE_SUCCESS);

		// start process
		String command = request.getRequestURI();
		if (command != null) {
			if (command.endsWith("/")) {
				command = command.substring(0, command.length() - 1);
			}
			int i = command.lastIndexOf('/');

			if (i >= 0) {
				command = command.substring(i + 1);
			}
		}
		if (command != null) {
			response.command = command;
		}
		// String tid = request.getHeader(VssConstants.TRANSACTION_ID);
		// // String sid = request.getHeader(VssConstants.SESSION_ID);
		// String addr = request.getHeader(VssConstants.CLIENT_ADDRESS);
		//
		// if (tid != null && !"".equals(tid)) {
		// response.setTransactionId(tid);
		// }
		// if (addr != null && !"".equals(addr)) {
		// int i = addr.indexOf(':');
		// if (i > 0) {
		// response.setClientHost(addr.substring(0, i));
		// try {
		// response.setClientPort(Integer.parseInt(addr
		// .substring(i + 1)));
		// } catch (NumberFormatException e) {
		// response.setClientPort(-1);
		// }
		// }
		// }
		return response;
	}

	public static Response createResponse(HttpServletRequest request,
			String returnCode, String returnMessage) {
		Response response = createResponse(request);
		response.setReturnCode(returnCode);
		response.setReturnMessage(returnMessage);
		return response;
	}
	public static Response createResponse(HttpServletRequest request,
			String returnCode, String returnMessage, String[] fields) {
		Response response = createResponse(request);
		response.setReturnCode(returnCode);
		response.setReturnMessage(returnMessage);
		response.setFields(fields);
		return response;
	}
	// public String getClientHost() {
	// return clientHost;
	// }
	//
	// public int getClientPort() {
	// return clientPort;
	// }

	public String getCommand() {
		return command;
	}

	public Object getParameters() {
		return parameters;
	}

	public String getReturnCode() {
		return returnCode;
	}

	public String getReturnMessage() {
		return returnMessage;
	}

	// public String getSessionId() {
	// return sessionId;
	// }
	//
	// public String getTransactionId() {
	// return transactionId;
	// }
	//
	// public void setClientHost(String clientHost) {
	// this.clientHost = clientHost;
	// }
	//
	// public void setClientPort(int clientPort) {
	// this.clientPort = clientPort;
	// }

	public void setCommand(String command) {
		this.command = command;
	}

	public void setParameters(Object parameters) {
		this.parameters = parameters;
	}

	public void setReturnCode(String returnCode) {
		this.returnCode = returnCode;
	}

	public void setReturnMessage(String returnMessage) {
		this.returnMessage = returnMessage;
	}

	// public void setSessionId(String sessionId) {
	// this.sessionId = sessionId;
	// }
	//
	// public void setTransactionId(String transactionId) {
	// this.transactionId = transactionId;
	// }

}
